// 翻译接口(或有道的接口写道这里来)
import {
  cryptoJs
} from './../assets/crypto-js';
import { jsonp } from "./jsonp";

function truncate(q) {
  let len = q.length;
  if(len<=20) return q;
  return q.substring(0, 10) + len + q.substring(len-10, len);
}
// crypto.CryptoJS
let crypto = {};
cryptoJs(null,function (obj) {
  crypto = obj;
});
const appKey = '34c888ea89330fad';
const key = 'JNMqwdnOICD7HzCQzktypYkQYBP0AITE';

const fanyi = (query,from = 'en',to = 'zh-CHS') => {
  let d = new Date();
  return jsonp('https://openapi.youdao.com/api',{
        q: query,
        appKey: appKey,
        salt: d.getTime(),
        from: from,
        to: to,
        sign: crypto.SHA256(appKey + truncate(query) + d.getTime() + Math.round(d.getTime()/1000) + key).toString(crypto.enc.Hex),
        signType: "v3",
        curtime: Math.round(d.getTime()/1000),
      });
};

export {
  fanyi
}

